package com.person.ntv.controller.sys;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.person.common.InitApplication;
import com.person.common.PageParams;
import com.person.exception.ServiceException;
import com.person.ntv.entity.sys.SysCode;
import com.person.ntv.entity.sys.SysCodeset;
import com.person.ntv.service.sys.CommonService;
import com.person.ntv.service.sys.SysCodeSetService;

@Controller
@RequestMapping("/system/sysCodeSet")
public class SysCodeSetController {
	@Autowired
	private SysCodeSetService  sysCodeSetService ;
	@Autowired
	private CommonService commonService ;
	
	@RequestMapping("/dataDicPage")
	public String  getdataDicPage(){
		return "/system/code/dataDic";
	}
	
	@RequestMapping("/listPage")
	public String  getCodeSetPage(ModelMap mv){
	    initCode(mv);
		return "/system/code/codeSetList";
	}
	
	@RequestMapping("/codeSetList")
	@ResponseBody
	public Map<String,Object> getCodeList(PageParams myPage) throws Exception{
		Map<String,Object> map=new HashMap<String,Object>();
		Page<SysCodeset> page=sysCodeSetService.getCodeSetPage(myPage);
        map.put("total",page.getTotal());
        map.put("rows",page.getResult());
		return map;
	}
	
	@RequestMapping("/addPage")
	public String  addCodeSetPage(ModelMap mv) throws Exception{
		initCode(mv);
		return "/system/code/codeSetAdd";
	}
	@RequestMapping("/editPage")
	public String  editCodeSetPage(String id,ModelMap mv) throws Exception{
		try {
	    initCode(mv);
	    SysCodeset codeSet =sysCodeSetService.selectByPrimaryKey(new BigDecimal(id));
	    mv.addAttribute("codeSet",codeSet);
		} catch (ServiceException e) {
			mv.addAttribute("error", "1");
			mv.addAttribute("msg",e.getMessage()+"查询出错！");
		}
		return "/system/code/codeSetEdit";
	}
	
	@RequestMapping("/addCodeSet")
	public String  addCodeSet(ModelMap mv,SysCodeset sysCodeSet) throws Exception{
		try {
			sysCodeSetService.insertSelective(sysCodeSet);
			InitApplication.codeMap=codeSetList();
			mv.addAttribute("error", "0");
			mv.addAttribute("msg","保存成功！");
		} catch (ServiceException e) {
			mv.addAttribute("error", "1");
			mv.addAttribute("msg",e.getMessage()+"保存失败！");
		}
		return "/system/code/codeSetAdd";
	}
	
	@RequestMapping("/editCodeSet")
	public String  editCodeSet(ModelMap mv,SysCodeset sysCodeSet) throws Exception{
		try {
			sysCodeSetService.updateByPrimaryKey(sysCodeSet); 
			InitApplication.codeMap=codeSetList();
			mv.addAttribute("error", "0");
			mv.addAttribute("msg","保存成功！");
		} catch (ServiceException e) {
			mv.addAttribute("error", "1");
			mv.addAttribute("msg",e.getMessage()+"保存失败！");
		}
		return "/system/code/codeSetEdit";
	}
	
	@RequestMapping("/delCodeSet")
	@ResponseBody
	public Map<String,Object>  delCodeSet(BigDecimal[]ids,HttpServletRequest request) throws Exception{
		Map<String,Object> map=new HashMap<String,Object>();
		try {
			sysCodeSetService.deleteByPrimaryKeys(ids); 
			InitApplication.codeMap=codeSetList();
			map.put("error", "0");
			map.put("msg","删除成功！");
		} catch (ServiceException e) {
			map.put("error", "1");
			map.put("msg",e.getMessage()+"删除失败！");
		}
		return map;
	}
	
	@RequestMapping("/detailPage")
	public String  detailCodeSetPage(String id,ModelMap mv) throws Exception{
		try {
	    SysCodeset codeSet = sysCodeSetService.selectByPrimaryKey(new BigDecimal(id));
	    mv.addAttribute("codeSet",codeSet);
		} catch (ServiceException e) {
			mv.addAttribute("error", "1");
			mv.addAttribute("msg",e.getMessage()+"查询出错！");
		}
		return "/system/code/codeSetDetail";
	}
	
	@RequestMapping("/validateCnName")
	@ResponseBody
	public boolean validateCnName(SysCodeset sysCodeset) throws Exception{
		try {
			if(sysCodeset.getCnName()==null)sysCodeset.setCnName("");
			int cnt=sysCodeSetService.validateCsName(sysCodeset.getCnName(), sysCodeset.getId()==null?null:String.valueOf(sysCodeset.getId()));
			if(cnt==0)
			return true;
		} catch (ServiceException e) {
           e.printStackTrace();
		}
		return false;
	}
	
	@RequestMapping("/validateEnName")
	@ResponseBody
	public boolean validateEnName(SysCodeset sysCodeset) throws Exception{
		try {
			if(sysCodeset.getEnName()==null)sysCodeset.setEnName("");
			int cnt=sysCodeSetService.validateCsCode(sysCodeset.getEnName(), sysCodeset.getId()==null?null:String.valueOf(sysCodeset.getId())); 
			if(cnt==0)
			return true;
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	@RequestMapping("/getSysCodeBySet")
	@ResponseBody
	public List<SysCode> getSysCodeBySet(HttpServletRequest request,HttpServletResponse response){
		List<SysCode> list = commonService.getCodeList(request.getParameter("codeSet"),true);
		return list;
	}	
	
	private void initCode(ModelMap mv){
		mv.addAttribute("SysCodeSetStatus",JSONArray.fromObject(commonService.getCodeList("SysCodeSetStatus",true)).toString());
		mv.addAttribute("SysCodeSetType",JSONArray.fromObject(commonService.getCodeList("SysCodeSetType",true)).toString());
	}
	private  Map<String,List<SysCode>>codeSetList()throws Exception {
		List<Map<String,Object>> list = commonService.getCodeSetList();
		Map<String,List<SysCode>> data=new HashMap<String,List<SysCode>>();
		SysCode code=null;
		List<SysCode> codeList=null;
		for(Map<String,Object>map:list){
			codeList=data.get(String.valueOf(map.get("enName")));
			if(codeList==null){
				codeList=new ArrayList<SysCode>();
			}
			code=new SysCode();
			code.setId(new BigDecimal(String.valueOf(map.get("id"))));
			code.setItemName(String.valueOf(map.get("itemName")).trim());
			code.setItemValue(String.valueOf(map.get("itemValue")).trim());
			code.setParentItemId(String.valueOf(map.get("parentItemId")));
			codeList.add(code);
			data.put(String.valueOf(map.get("enName")),codeList);
		}
		return data;
	}
}
